﻿using System;
using DotNet.SerializationDemo.Models;
using DotNet.SerializationDemo.Services;

namespace DotNet.SerializationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            /*****************************************************************************
             *  
             *  This demonstrates seializing a complex object to xml
             *  
             *  If I'm serializing something, I'll almost always use xml unless I'm given 
             *  a good reason not to. Xml is very forgiving, easy to read, and easy to 
             *  manually modify. In addition, it cooperates well with SQL Server since you
             *  can actually query XML fields. 
             *  
             *  I strip out the schema info and xml namespace, as most of the time is just 
             *  really not important to the xml I work with and it tends to clutter the xml. 
             *  Were I passing XML to another company or were I really woried about the 
             *  integrity of the XML structure, I wouldn't hesitate to leave it in. This is 
             *  not something I would recommend doing for professional, distributed 
             *  applications. 
             *  
             * ***************************************************************************/

            var model = new Person
            {
                FirstName = "Joe",
                LastName = "Somebody",
                Age = 32,
                Address = new Address
                {
                    Number = 1234,
                    Street = "Main St.",
                    City = "Louisville",
                    State = State.KY
                },
                PhoneNumbers = new[]
                {
                    "502-123-4567",
                    "502-987-6543"
                }
            };

            //In real use, I would bring this into a class via dependency injection
            IXmlSerializationService serializationService = new XmlSerializationService();

            var xml = serializationService.Serialize(model);

            //Breakpoint here checkout the xml
            //you can use the XML Visualizer for a pretty, well formatted view
            Console.WriteLine(xml);

            var deserializedObject = serializationService.Deserialize<Person>(xml);

            //Breakpoint here can checkout the deserialized object
            //as you can see it's exactly the same as what we serialized. 
            Console.WriteLine("{0} {1} has been deserialized", deserializedObject.FirstName, deserializedObject.LastName);

            Console.ReadLine();
        }
    }
}
